package com.ygt.tomcat;

import com.ygt.servlet.HttpServletResponse;
import com.ygt.util.FileUtil;
import com.ygt.util.ResponseUtil;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @Author ygt
 * @Date 2022/11/17 17:40
 * @Version 1.0
 */
public class Response implements HttpServletResponse {

    //创建输出流
    private OutputStream outputStream;

    public Response(OutputStream outputStream){
        this.outputStream = outputStream;
    }

    @Override
    public void GetWriter(String context) throws IOException {
        outputStream.write(context.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 返回静态资源
     * @param path
     */
    public void GetWriterHtml(String path) throws Exception {
        //获取resources下面文件的路径
        String resourcesPath = FileUtil.getResoucePath(path);
        File file = new File(resourcesPath);
        if (file.exists()){
            System.out.println("静态资源存在");
            FileUtil.writeFile(file,outputStream);
        }else {
            GetWriter(ResponseUtil.getResponseHeader404());
        }
    }
}
